package com.android.meige.fragment.member;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.meige.R;
import com.android.meige.activity.BaseObject;
import com.android.meige.activity.mine.RefundDetailActivity;
import com.android.meige.adapt.listMinOrderRefundAdapt;
import com.android.meige.model.member.OrderRefoundData;
import com.android.meige.model.member.OrderRefoundDataCallback;
import com.android.meige.utils.ConstantsUrl;
import com.android.meige.utils.HttpUtils;
import com.android.meige.utils.LoginState;
import com.android.meige.utils.MyLog;
import com.android.meige.utils.ObjectUtils;
import com.android.meige.view.group.VRefreshLayout;
import com.android.meige.view.recycleview.CustomLoadMoreView;
import com.android.meige.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 退款列表
 * ---------------------------------------
 */
public class Fragment_Mine_OrderRefound extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    VRefreshLayout mRefreshLayout;
    @BindView(R.id.top)
    ImageView top;
    @BindView(R.id.click_resetnetwork_msg)
    TextView clickResetnetworkMsg;
    @BindView(R.id.click_resetnetwork_refresh)
    TextView clickResetnetworkRefresh;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_img)
    ImageView noDataImg;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;

    private View rootView;
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private FragmentActivity mcontext;
    private listMinOrderRefundAdapt adapter;
    //控件是否已经初始化
    private boolean isPrepared;
    private LayoutInflater inflater;
    private int page = 1;
    private boolean networkConnected;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    public static Fragment_Mine_OrderRefound newInstance(String param1) {
        Fragment_Mine_OrderRefound fragment = new Fragment_Mine_OrderRefound();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        rootView = inflater.inflate(R.layout.layout_recycle_refresh, container, false);
        ButterKnife.bind(this, rootView);
        mcontext = getActivity();
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        mainTopBg.setVisibility(View.GONE);
        initUI();
        isPrepared = true;
        setlazyLoad();
        return rootView;
    }

    private void initUI() {
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new listMinOrderRefundAdapt(R.layout.item_list_mine_order, null);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        final LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                Intent intent = new Intent(mcontext, BaseObject.class);
                intent.putExtra("title", "订单详情");
                intent.putExtra("need_id", adapter.getItem(position).getOrder_sn());
                mcontext.startActivity(intent);
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public boolean onItemChildClick(BaseQuickAdapter adapter1, View view, final int position) {
                OrderRefoundData.DataBean.ListBean listBean = adapter.getItem(position);
                switch (view.getId()) {

                    case R.id.item_list_mine_dingdang_refound_apply:

                        Intent intent = new Intent(mcontext, RefundDetailActivity.class);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_ID, listBean.getRefund_id() + "");
                        if (listBean.getStatus() == 10 && listBean.getStatus_shipping() == 0 && listBean.getType() == 2) {
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_TYPE, 1);
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_NUMBER, 1);

                        } else if (listBean.getStatus() == 10 && listBean.getStatus_shipping() == 1 && listBean.getType() == 2) {
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_TYPE, 1);
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_NUMBER, 2);
                        } else if (listBean.getStatus() == 2) {
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_TYPE, 2);
                        }
                        startActivity(intent);
                        break;
                    case R.id.item_list_mine_dingdang_refound_contact:
                        ObjectUtils.toast(mcontext, "联系卖家");
                        break;
                    case R.id.item_list_mine_dingdang_refound_detail:
                        Intent intent_detail = new Intent(mcontext, RefundDetailActivity.class);
                        intent_detail.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_ID, listBean.getRefund_id() + "");
                        intent_detail.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_TYPE, 0);
                        startActivity(intent_detail);
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected void setlazyLoad() {
        super.setlazyLoad();
        if (!isPrepared || !isVisible) {
            return;
        }
        page = 1;
        mRecyclerView.scrollToPosition(0);
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            GetDataListData(page);
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
        //刷新
        ReFreshData();
    }

    private void ReFreshData() {
        if (mRefreshLayout != null) {
            mRefreshLayout.setBackgroundColor(Color.TRANSPARENT);
            mRefreshLayout.setAutoRefreshDuration(400);
            mRefreshLayout.setRatioOfHeaderHeightToReach(1.5f);
            mRefreshLayout.addOnRefreshListener(new VRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    networkConnected = ObjectUtils.isNetworkConnected(mcontext);
                    if (networkConnected) {
                        mRefreshLayout.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                page = 1;
                                GetDataListData(page);
                                mRefreshLayout.refreshComplete();
                            }
                        }, 1000);
                    } else {
                        ObjectUtils.toast(mcontext, "网络连接出现异常");
                    }
                }
            });
        }
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        page = 1;
        GetDataListData(page);
    }

    //获取列表数据
    private void GetDataListData(final int page) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDERTUIKUANG + "?page=" + page;
            MyLog.e("地址", address);
            HashMap<String, String> header = new HashMap<>();
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            header.put("UID", getid);
            header.put("UPSW", getpd);

            HttpUtils.get(address, header, null, new OrderRefoundDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(OrderRefoundData response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        OrderRefoundData.DataBean data = response.getData();
                        if (data == null || !ObjectUtils.listHasData(data.getList())) {
                            if (page == 1) {
                                noData.setVisibility(View.VISIBLE);
                            } else {
                                adapter.loadMoreEnd();
                            }
                            return;
                        }
                        ArrayList<OrderRefoundData.DataBean.ListBean> listdata = new ArrayList<>();
                        List<OrderRefoundData.DataBean.ListBean> list = data.getList();
                        listdata.addAll(list);
                        if (page == 1) {
                            adapter.setNewData(listdata);
                        } else {
                            adapter.addData(listdata);
                            adapter.loadMoreComplete();
                        }
                    } else {
                        if (page == 1) {
                            noData.setVisibility(View.VISIBLE);
                        } else {
                            adapter.loadMoreFail();
                            ObjectUtils.toast(mcontext, response.getMsg());
                        }
                    }
                }
            });
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络连接出现异常");
            adapter.loadMoreFail();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        GetDataListData(page);
    }
}
